package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivitySelVideoBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelVideoActivity extends BaseAc<ActivitySelVideoBinding> {

    public static int type;
    private SelVideoAdapter videoAdapter;
    private int oldPosition = 0;
    private String selPath = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_video;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSelVideoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSelVideoImport.setOnClickListener(this);
        mDataBinding.rvSelVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        videoAdapter = new SelVideoAdapter();
        mDataBinding.rvSelVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSelVideoImport://导入视频
                if (TextUtils.isEmpty(selPath)) {
                    ToastUtils.showShort(R.string.please_sel_video);
                    return;
                }
                if (type == 0) {
                    VideoSpeedActivity.videoPath = selPath;
                    startActivity(VideoSpeedActivity.class);
                } else if (type == 1) {
                    VideoUpendActivity.videoPath = selPath;
                    startActivity(VideoUpendActivity.class);
                } else if (type == 2) {
                    AddTextActivity.videoPath = selPath;
                    startActivity(AddTextActivity.class);
                } else if (type == 3) {
                    AddStickerActivity.videoPath = selPath;
                    startActivity(AddStickerActivity.class);
                } else if (type == 4) {
                    VideoCompressActivity.videoPath = selPath;
                    startActivity(VideoCompressActivity.class);
                } else if (type == 5) {
                    VideoFormatActivity.videoPath = selPath;
                    startActivity(VideoFormatActivity.class);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        videoAdapter.getItem(oldPosition).setChecked(false);
        videoAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        videoAdapter.getItem(position).setChecked(true);
        videoAdapter.notifyItemChanged(position);
        selPath = videoAdapter.getItem(position).getPath();
        mDataBinding.tvSelVideoImport.setText(R.string.sel_video_text2);
    }

    @Override
    protected void initData() {
        getData();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                        mDataBinding.tvSelVideoNoData.setVisibility(View.VISIBLE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> list = new ArrayList<>();
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                if (listSys != null || listSys.size() > 0) {
                    list.addAll(listSys);
                }
                emitter.onNext(list);
            }

            @Override
            public void accept(List<SelectMediaEntity> list) {
                if (list != null && list.size() > 0) {
                    videoAdapter.setList(list);
                    mDataBinding.rvSelVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                    mDataBinding.tvSelVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}